package com.fingard.dsp.bank.directbank.cib02;

import com.fingard.dsp.bank.directbank.bankInterface.IQuotaInfo;
import com.fingard.dsp.bank.reqrespobj.qryquotainfo.ReqQryQuotaInfo;
import com.fingard.dsp.bank.reqrespobj.qryquotainfo.RetAtsQuotaInfo;
import com.fingard.dsp.bank.reqrespobj.qryquotainfo.RetAtsQuotaInfoDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 获取额度信息
 *
 * @author Parker on 2019-11-18.
 * @version 1.0
 */
public class CIB02QryQuotaInfo extends CIB02Base implements IQuotaInfo {
    @Override
    public RetAtsQuotaInfo queryQuotaInfo(ReqQryQuotaInfo reqQryQuotaInfo) throws Exception {
        RetAtsQuotaInfo retAts = new RetAtsQuotaInfo(reqQryQuotaInfo.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader(xmlWriter);
            xmlWriter.writeStartElement("REQUEST");
            xmlWriter.writeElementString("TRANCODE", "18001");//交易代码
            xmlWriter.writeElementString("COREID", bankFront.org);//直联企业系统客户号
            xmlWriter.writeElementString("COTYPE", getBankConfig().getValueAsString("coType"));//对接企业类型 1核心企业2服务商
            xmlWriter.writeElementString("SCOCOD", "");//核心企业组织机构代码
            xmlWriter.writeElementString("CERTID", "");//客户组织机构代码
            xmlWriter.writeElementString("TPCODE", reqQryQuotaInfo.DealerCode);//企业外部编码
            xmlWriter.writeElementString("CRDTID", "");//授信额度编号
            xmlWriter.writeElementString("CRESTS", "");//额度状态
            xmlWriter.writeElementString("PROTPE", "01");//产品类型 01 付款代理02 汽车合格证融资
            xmlWriter.writeElementString("REV50L", "");//保留字段
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),"T18001");//发送银行

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/RESPONSE/RTNCOD");
                    String tmpRetMsg = tmpBkReader.getFirstText("/RESPONSE/RTNMSG");
                    if (StringHelper.isNullOrEmpty(tmpRetCode)) {
                        tmpRespCode = "0";
                        tmpRespInfo = "获取成功";
                        RetAtsQuotaInfoDetail tmpRetDetail = new RetAtsQuotaInfoDetail();
                        tmpRetDetail.DealerCode = reqQryQuotaInfo.DealerCode;
                        tmpRetDetail.LimitBeginDate = tmpBkReader.getFirstText("/RESPONSE/STADAT");
                        tmpRetDetail.LimitEndDate = tmpBkReader.getFirstText("/RESPONSE/ENDDAT");
                        tmpRetDetail.Limit = tmpBkReader.getFirstText("/RESPONSE/CRDAMT");
                        tmpRetDetail.UsedLimit = tmpBkReader.getFirstText("/RESPONSE/CUDAMT");
                        tmpRetDetail.UnuseLimit = tmpBkReader.getFirstText("/RESPONSE/CAVAMT");
                        tmpRetDetail.Reserve1 = "";
                        tmpRetDetail.Reserve2 = "";
                        tmpRetDetail.Reserve3 = "";
                        tmpRetDetail.Reserve4 = "";
                        tmpRetDetail.Reserve5 = "";
                        retAts.writeRetDetail(tmpRetDetail);
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpRetMsg;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }


        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}
